package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.dao.SurveyContentDao;
import model.dao.SurveyItemDao;
import model.entity.SurveyItem;

/**
 * Servlet implementation class GetSurCont
 */
@WebServlet("/GetSurCont")
public class GetSurCont extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetSurCont() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SurveyItemDao sid=new SurveyItemDao();
		SurveyContentDao scd=new SurveyContentDao();
		String str=null;
		String content=new String("content");
		String itemid=new String("item_id");
		List<SurveyItem> listItem=sid.getAllSelect();
		str="[";
		for (SurveyItem si : listItem) {
			Object idVal=new Integer(si.getId());
			List<Object> listCont=scd.getSigleSelect(content, itemid, idVal);
			str+="{\"id\":" + si.getId() + ",\"name\":\"" + si.getName() + "\",\"content\":[";
			for (Object sc : listCont) {
				str+="{\"name\":\"" + sc + "\"},";
			}
			str=str.substring(0, str.length()-1);
			str+="]},";
		}
		str=str.substring(0,str.length()-1);
		str+="]";
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		System.out.println(str);
		out.print(str);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
